package com.xinghen.validate;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * @author xinghen
 * @ClassName: ValidateAspect
 * @Package: com.xinghen.validate
 * @Description TODO
 * @create 2018/04/08 21:04
 */
@Aspect
@Component
public class ValidateAspect {

    @Around("execution(* com.xinghen.web.controller.*.*(..))")
    public void handleValidateResult(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof BindingResult) {
                BindingResult bindingResult = (BindingResult) arg;
                if (bindingResult.hasErrors()) {
                    throw new ValidateException(bindingResult.getAllErrors());
                }
            }
        }
    }

}
